package mylittleinvestigations.Screens;

import java.util.logging.Level;
import java.util.logging.Logger;
import mylittleinvestigations.Events.ImageButtonEventListener;
import mylittleinvestigations.Events.ImageButtonEventProvider;
import mylittleinvestigations.GameUi.ImageButton;
import mylittleinvestigations.PersistentThreads.RenderThread;
import mylittleinvestigations.Settings;
import org.dawnmew.jvirtualstream.VirtualFileSystem;
import org.lwjgl.LWJGLException;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class TitleScreen extends Screen implements ImageButtonEventListener
{
    public static final String Id = "TitleScreen";
    
    private Image backgroundImage;
    private ImageButton newGameButton;
    private ImageButton voicesOnButton;
    private ImageButton voicesOffButton;
    
    public TitleScreen()
    {
        /*while (!RenderThread.getInstance().IsReady())
        {
            try
            {
                Thread.sleep(10);
            }
            catch (InterruptedException ex)
            {
                Logger.getLogger(TitleScreen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }*/
        
        try
        {
            //RenderThread.getSharedContext().makeCurrent();
            
            this.backgroundImage = new Image(VirtualFileSystem.getInstance().getInputStream("image/TitleScreenBackground.png"), "image/TitleScreenBackground.png", false);
            this.newGameButton =
                new ImageButton(
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/NewGameMouseOff.png"), "image/NewGameMouseOff.png", false),
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/NewGameMouseOver.png"), "image/NewGameMouseOver.png", false),
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/NewGameMouseDown.png"), "image/NewGameMouseDown.png", false),
                    383,
                    307
                );
            this.voicesOnButton =
                new ImageButton(
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/VoicesOnMouseOff.png"), "image/VoicesOnMouseOff.png", false),
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/VoicesOnMouseOver.png"), "image/VoicesOnMouseOver.png", false),
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/VoicesOnMouseDown.png"), "image/VoicesOnMouseDown.png", false),
                    383,
                    362
                );
            this.voicesOffButton =
                new ImageButton(
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/VoicesOffMouseOff.png"), "image/VoicesOffMouseOff.png", false),
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/VoicesOffMouseOver.png"), "image/VoicesOffMouseOver.png", false),
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/VoicesOffMouseDown.png"), "image/VoicesOffMouseDown.png", false),
                    this.voicesOnButton.getXPosition(),
                    this.voicesOnButton.getYPosition()
                );
            
            //RenderThread.getSharedContext().releaseContext();
        }
        /*catch (LWJGLException ex)
        {
            Logger.getLogger(TitleScreen.class.getName()).log(Level.SEVERE, null, ex);
        }*/
        catch (SlickException ex)
        {
            Logger.getLogger(TitleScreen.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        ImageButtonEventProvider.RegisterListener(this);
    }
    
    @Override
    public void Init()
    {
        this.isFinished = false;
        this.nextScreenId = null;
    }

    public void Update(GameContainer gc, int delta)
    {
        this.newGameButton.Update(gc, delta);
        
        if (Settings.PlayDialogAudio)
        {
            this.voicesOnButton.Update(gc, delta);
        }
        else
        {
            this.voicesOffButton.Update(gc, delta);
        }
    }

    public void Draw(GameContainer gc, Graphics g)
    {
        RenderThread.getInstance().AddImageToRenderQueue(this.backgroundImage, 0, 0);
        this.newGameButton.Draw(gc, g);
        
        if (Settings.PlayDialogAudio)
        {
            this.voicesOnButton.Draw(gc, g);
        }
        else
        {
            this.voicesOffButton.Draw(gc, g);
        }
    }

    public void OnButtonClicked(ImageButton sender)
    {
        if (sender == this.newGameButton)
        {
            this.nextScreenId = GameScreen.Id;
            this.isFinished = true;
        }
        else if (sender == this.voicesOnButton)
        {
            Settings.PlayDialogAudio = false;
        }
        else if (sender == this.voicesOffButton)
        {
            Settings.PlayDialogAudio = true;
        }
    }
}
